package com.yingxin.yxerp.module.dingtalk.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
import com.taobao.api.ApiException;
import com.yingxin.yxerp.module.dingtalk.config.AppConfig;
import com.yingxin.yxerp.module.dingtalk.constant.UrlConstant;
import com.yingxin.yxerp.module.dingtalk.service.IDingDeptBaseService;
import com.yingxin.yxerp.module.dingtalk.util.AccessTokenUtil;
import com.yingxin.yxerp.module.dingtalk.vo.DingDeptBaseRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 钉钉获取部门信息 实现类
 * yuebao
 * 23-04-14
 */

@Slf4j
@Service
public class DingDeptBaseServiceImpl implements IDingDeptBaseService {

    @Autowired
    private AppConfig appConfig;

    @Override
    public List<DingDeptBaseRespVo> getDeptBaseList() throws ApiException {
        // 获取 AccessToken
        String accessToken = AccessTokenUtil.getAccessToken( appConfig.getAppKey(), appConfig.getAppSecret() );

        // 返回结果集
        List<DingDeptBaseRespVo> depts = new ArrayList<>();

        getDeptBaseListsubId( Long.parseLong( "1" ), depts, accessToken );

        // 获取部门详情
        for( DingDeptBaseRespVo dept : depts ) {
            getDepartmentByIdInfo( dept, accessToken );
        }

        for( DingDeptBaseRespVo dept : depts ) {
            System.out.println(
                " 部门ID = " + dept.getDeptId() +
                ", 部门名称 = " + dept.getName() +
                ", 上级部门ID = " + dept.getParentId() );
        }

        return depts;
    }

    /**
     * 获取部门详情信息
     * @param dept
     */
    private void getDepartmentByIdInfo(DingDeptBaseRespVo dept, String access_token) throws ApiException {
        DefaultDingTalkClient client = new DefaultDingTalkClient( UrlConstant.GET_DEPARTMENT_INFO_URL );
        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
        req.setDeptId( dept.getDeptId() );
        req.setLanguage("zh_CN");
        OapiV2DepartmentGetResponse rsp = client.execute(req, access_token);
        if( ObjectUtil.isEmpty( rsp ) ){
            return;
        }

        OapiV2DepartmentGetResponse.DeptGetResponse deptGetResponse = rsp.getResult();
        if( ObjectUtil.isEmpty( deptGetResponse ) ){
            return;
        }

        dept.setDeptId( deptGetResponse.getDeptId() );
        dept.setName( deptGetResponse.getName() );
        dept.setParentId( deptGetResponse.getParentId() );
        dept.setSourceIdentifier( deptGetResponse.getSourceIdentifier() );
        dept.setCreateDeptGroup( deptGetResponse.getCreateDeptGroup() );
        dept.setAutoAddUser( deptGetResponse.getAutoAddUser() );
        dept.setAutoApproveApply( deptGetResponse.getAutoApproveApply() );
        dept.setFromUnionOrg( deptGetResponse.getFromUnionOrg() );
        dept.setTags( deptGetResponse.getTags() );
        dept.setOrder( deptGetResponse.getOrder().toString() );
        dept.setDeptGroupChatId( deptGetResponse.getDeptGroupChatId() );
        dept.setGroupContainSubDept( deptGetResponse.getGroupContainSubDept() );
        dept.setOrgDeptOwner( deptGetResponse.getOrgDeptOwner() );
        dept.setDeptManagerUseridList( deptGetResponse.getDeptManagerUseridList() );
    }

    /**
     * 获取部门子部门列表
     */
    private void getDeptBaseListsubId(Long deptId, List<DingDeptBaseRespVo> depts, String accessToken ) throws ApiException {
        // 获取子部门列表
        DefaultDingTalkClient client = new DefaultDingTalkClient( UrlConstant.GET_DEPT_LISTSUBID_URL );
        OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
        req.setDeptId( deptId );
        OapiV2DepartmentListsubidResponse rsp = client.execute( req, accessToken );
        OapiV2DepartmentListsubidResponse.DeptListSubIdResponse deptListSubIdResponse = rsp.getResult();
        if( ObjectUtil.isNotEmpty( deptListSubIdResponse ) ) {
            List<Long> deptIdList = deptListSubIdResponse.getDeptIdList();
            if( CollectionUtil.isNotEmpty( deptIdList ) ){
                for (Long l : deptIdList ) {
                    DingDeptBaseRespVo dept = new DingDeptBaseRespVo();
                    dept.setDeptId( l );

                    depts.add(dept);

                    // 获取下级部门信息
                    getDeptBaseListsubId( dept.getDeptId(), depts, accessToken );
                }
            }
        }
    }
}
